﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Windows;
using System.ComponentModel;

namespace Model.Events
{
    //represents dates within the game similar to DateTime
    public class GameDate : INotifyPropertyChanged
    {
        private int _Year = 666, _Month = 1, _Day = 1, _Hour = 1, _Minute = 1;

        public GameDate() 
        { 
            DateTime date = DateTime.Now;
            Year = date.Year;
            Month = date.Month;
            Day = date.Day;
            Hour = date.Hour;
            Minute = date.Minute;
        }

        public GameDate(int year, int month, int day, int hour, int minute)
        {
            Year = year;
            Month = month;
            Day = day;
            Hour = hour;
            Minute = minute;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public String GameDateProp
        {
            get { return this.ToString(); }
        }       

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {               
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public GameDate Increment()
        {
            Minute += 5;

            //NotifyPropertyChanged("GameDateProp");

            return this;
        }

        public int Year
        {
            get
            {
                return _Year;
            }
            set
            {
                if (value >= 0)
                    _Year = value;
                else
                    throw new ArgumentException();
                NotifyPropertyChanged("GameDateProp");
            }
        }

        public int Month
        {
            get
            {
                return _Month;
            }
            set
            {
                if (value > 12)
                {
                    _Month = value % 12;
                    Year += (int)(value / 12);
                }
                else if (value > 0)
                    _Month = value;
                else
                    throw new ArgumentException();
                NotifyPropertyChanged("GameDateProp");
            }
        }

        public int Day
        {
            get
            {
                return _Day;
            }
            set
            {
                if (value > 31)
                {
                    //assuming all months have 31 days
                    _Day = value % 31;
                    Month += (int)(value / 31);
                }
                else if (value > 0)
                    _Day = value;
                else
                    throw new ArgumentException();
                NotifyPropertyChanged("GameDateProp");
            }
        }

        public int Hour
        {
            get
            {
                return _Hour;
            }
            set
            {
                if (value > 23)
                {
                    _Hour = value % 23;
                    Day += (int)(value / 23);
                }
                else if (value > 0)
                    _Hour = value;
                else
                    throw new ArgumentException();
                NotifyPropertyChanged("GameDateProp");
            }
        }

        public int Minute
        {
            get
            {
                return _Minute;
            }
            set
            {
                if (value > 59)
                {
                    _Minute = value % 60;
                    Hour += (int)(value / 60);
                }
                else if (value > 0)
                    _Minute = value;
                else
                    throw new ArgumentException();
                NotifyPropertyChanged("GameDateProp");
            }
        }

        //for internal testing only
        public static void Test()
        {
            GameDate date = new GameDate(2008, 8, 31, 21, 14);

            while (true)
            {
                date.Increment();
                Console.WriteLine(date);
                System.Threading.Thread.Sleep(200);
                Console.Clear();
            }
        }

        public override string ToString()
        {
            //http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
            DateTime Date = DateTime.Now;
            try
            {
                Date = new DateTime(Year, Month, Day, Hour, Minute, 0);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception caught: " + e + "\n" + Year + " " + Day + "/" + Month + " " + Hour + ":" + Minute);
            }
            CultureInfo culture = new CultureInfo("en-GB");
            //evt. brug custom format
            return Date.ToString("f", culture);
        }
    }
}
